﻿using MyFile.Config;
using SqlSugar;
using System.Data;
using System.Data.SqlClient;

namespace MyFile.Utils
{
    public class DBHelper
    {
        private static readonly Lazy<DBHelper> lazy = new Lazy<DBHelper>(() => new DBHelper());
        public static DBHelper Instance { get { return lazy.Value; } }
        public SqlSugarClient mDB;
        public readonly static string Connection = $"server={Program.SqlIp};database=MyFile;uid=root;pwd=Youzi520";

        public void InitDB()
        {
            try
            {
                mDB = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = Connection,
                    DbType = SqlSugar.DbType.MySql,
                    //自动释放数据事务
                    IsAutoCloseConnection = true,
                    //从实体特性中读取主键自增列信息
                    InitKeyType = InitKeyType.Attribute
                });
                mDB.Aop.OnLogExecuting = (sql, pars) =>
                {
                    //Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}：{sql}");
                    //Console.WriteLine($"-------------------------------------------------------------------------------------------------");
                };
                //如果该数据库不存在，则创建
                mDB.DbMaintenance.CreateDatabase();
                //创建表
                mDB.CodeFirst.InitTables(typeof(DBUU));
                mDB.CodeFirst.InitTables(typeof(DBGoodsList));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    class DBHelperTwo
    {
        public static string sql = "server=.;database=GeRen;integrated security=true";
        public static SqlConnection sqlconn = null;
        public static void InitConnection()
        {
            if (sqlconn == null)
            {
                sqlconn = new SqlConnection(sql);
            }
            if (sqlconn.State == ConnectionState.Closed)
            {
                sqlconn.Open();
            }
            if (sqlconn.State == ConnectionState.Broken)
            {
                sqlconn.Close();
                sqlconn.Open();
            }
        }
        public static SqlDataReader GetDataReader(string sql)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sql, sqlconn);
            SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return dataReader;
        }
        public static bool GetExecuteNonQuery(string sql)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sql, sqlconn);
            int result = cmd.ExecuteNonQuery();
            sqlconn.Close();
            return result > 0;
        }
        public static object GetExecuteScalar(string sql)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sql, sqlconn);
            object result = cmd.ExecuteScalar();
            sqlconn.Close();
            return result;
        }
        public static DataSet GetDataSet(string sql)
        {
            InitConnection();
            DataSet ds = new DataSet();
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sql, sqlconn);
            da.Fill(ds);
            sqlconn.Close();
            return ds;
        }
    }
}
